<template>
  <div style="display: flex; flex-direction: column;">
    <div class="panel-title">
      <span class="panel-title-text">新鲜事</span>
    </div>
    <ul class="feed-list">
      <li v-for="(item) in feed_list" :key="item.ts" :class="[item.done ? 'feed-done' : null ]" >
        <i v-if="item.delete" class="feed-icon">❌</i>
        <i v-else-if="!item.done" class="feed-icon">⚠️</i>
        <i v-else-if="item.done" class="feed-icon">✅</i>
        <!-- <img class="avatar" src="../../assets/image/img1.jpg" > -->
        <div class="feed-list-info" >
          <div class="feed-title" >
            <span class="feed-title-main" >
              <span class="feed-title-text">{{item.title}}</span>
              <span v-if="!item.done  && item.dt_to > Date.now()" class="feed-last-time" >{{item.to_now }}</span>
                <span v-if="!item.done && item.dt_to < Date.now()" class="feed-pass-time" >{{item.to_now }}过期</span>
            </span>
          </div>
          <div class="feed-content" >{{item.content}}</div>
          <div class="feed-time">
            {{item.weekday}} {{item.dt_to | slimdatetime}}
          </div>
        </div>
        <button class="btn" @click="onAddToMine(item)" >添加</button>

      </li>
    </ul>
  </div>
</template>

<script>
import moment from 'moment'
import db from '@/modules/common/db.js'
const weekdays = [
  '', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日'
]

let refresh = [1]
export let updateTimeline = function (d) {
  let timeline = db.get('timeline_list', [])
  d.ts = Date.now()
  timeline.unshift(d)
  if (timeline.length > 50) {
    timeline = timeline.slice(0, 49)
  }
  db.set('timeline_list', timeline)
  console.log('refreshaaa')
  refresh.pop()
  refresh.push(Date.now())
}
export default {
  name: 'TaskTimeline',
  props: ['current_category'],
  components: {
  },
  data () {
    return {
      refresh: refresh,
      feed_list: []
    }
  },
  mounted () {
    this.load()
  },
  watch: {
    refresh: function () {
      console.log('refresh')
      this.load()
    }
  },
  methods: {
    load: function () {
      let td = db.get('timeline_list', [])
      for (let i in td) {
        td[i].index = i
        td[i].weekday = weekdays[moment(td[i].dt_to).weekday()]
        td[i].to_now = moment(td[i].dt_to).fromNow()
      }
      this.feed_list = td
    },
    onAddToMine: function (item) {
      if (!this.current_category.id) {
        return alert('未选择任务分类')
      }
      item.id = Date.now()
      item.delete = false
      item.done = false
      let ls = db.get('todo_list#' + this.current_category.id, [])
      ls.unshift(item)
      updateTimeline(item)
      db.set('todo_list#' + this.current_category.id, ls)
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

.avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  margin-right: 10px;
}

.feed-icon {
  margin-right: 10px;
  font-size: 20px;
  font-style: normal;
}

.feed-list {
  font-size: 16px;
  /* font-weight: bold; */
  padding: 20px;
  min-height: 600px;
}

.feed-list li {
  display: flex;
  flex-direction: row;
  align-items: center;
  text-align: left;
  padding: 8px 10px;
  /* background: #FFF; */
  margin: 5px auto;
  border-bottom: 1px solid #EEE;
}
.feed-list li:hover {
  background: #fdffea;
  cursor: pointer;
}

li.feed-done{
  background: #FAFAFA;
}
.feed-list-info {
  flex: 1;
}
.feed-title {
  display: flex;
}
.feed-title-main {
  flex: 1;
}
.feed-done .feed-title-text {
  text-decoration: line-through;
}

.feed-content {
  font-size: 14px;
  font-weight: normal;
  color: #888;
}

.feed-time {
  font-size: 14px;
  font-weight: normal;
  /* color: #888; */
}

.feed-last-time {
  font-size: 16px;
  color: red;
}
.feed-pass-time {
  font-size: 16px;
  color: gray;
}
</style>
